#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

void pipes()
{
    int pipefd[2];
    if (pipe(pipefd) == -1)
    {
        return;
    }
    pid_t pid = fork();
    if (pid == -1)
    {
        return;
    }
    else if (pid == 0)
    {
        char buf[1024];
        read(pipefd[0], buf, sizeof(buf));
        printf("child: %s\n", buf);
        close(pipefd[0]);
        close(pipefd[1]);
        exit(0);
    }
    else 
    {
        char buf[1024];
        sprintf(buf, "hello, world");
        write(pipefd[1], buf, strlen(buf));
        close(pipefd[0]);
        close(pipefd[1]);
        waitpid(pid, NULL, 0);
        exit(0);
    }
}

void fifo()
{
    const char* fth = "/tmp/myfifo";
    mkfifo(fth, 0666);
    pid_t pid = fork();
    if (pid == -1)
    {
        return;
    }
    else if (pid == 0)
    {
        int fd = open(fth, O_RDONLY);
    }
    else
    {
        int fd = open(fth, O_WRONLY);
        write(fd, "hello, world", 13);
        close(fd);
        waitpid(pid, NULL, 0);
        exit(0);
    }
}

struct{
    long mtype;
    char mtext[1024];
} msg;
void msgQueue()
{
    key_t k = ftok(".", 'a');
    int id = msgget(k, IPC_CREAT | 0666);
    msg.mtype = 0;
    strcpy(msg.mtext, "hello, world");
    msgsnd(id, &msg, sizeof(msg), 0);
    msgrcv(id, &msg, sizeof(msg), 0, 0);
    printf("msg: %s\n", msg.mtext);
    msgctl(id, IPC_RMID, NULL);

    key_t kk = ftok(".", 'b');
    int idd = shmget(kk, 1024, IPC_CREAT | 0666);
    char* shm = (char*)shmat(idd, NULL, 0);
    strcpy(shm, "hello, world");
    printf("shm: %s\n", shm);
    shmdt(shm);
    shmctl(idd, IPC_RMID, NULL);
    key_t kkk = ftok(".", 'c');
    int iddd = semget(kkk, 1, IPC_CREAT | 0666);
    semop(iddd, &sem, 1);
    printf("sem: %d\n", sem.val);
    semctl(iddd, 0, IPC_RMID, NULL);
    exit(0);
}

void skt1()
{
    int skid = socket(AF_INET, SOCK_STREAM, 0);
    int on = 1;
    setsockopt(skid, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8080);
    addr.sin_addr.s_addr = INADDR_ANY;
    bind(skid, (struct sockaddr*)&addr, sizeof(addr));
    listen(skid, 10);
    int connfd = accept(skid, NULL, NULL);
    char buf[1024];
    read(connfd, buf, sizeof(buf));
    printf("buf: %s\n", buf);
    close(connfd);
    close(skid);
    exit(0);
}

void skt2()
{
    int skid = socket(AF_INET, SOCK_DGRAM, 0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8080);
    addr.sin_addr.s_addr = INADDR_ANY;
    bind(skid, (struct sockaddr*)&addr, sizeof(addr));
    char buf[1024];
    recvfrom(skid, buf, sizeof(buf), 0, NULL, NULL);
    printf("buf: %s\n", buf);
    sendto(skid, buf, sizeof(buf), 0, (struct sockaddr*)&addr, sizeof(addr));
    close(skid);
    exit(0);
}

void skt3()
{
    
}
